#! /bin/env python

import sys

from BeautifulSoup import BeautifulSoup as Soup
from xml.dom.minidom import Document

def main(file):
    doc = Document()
    stationList = doc.createElement('stationList')
    doc.appendChild(stationList)

    soup = Soup(file)
    for item in soup.body.find('ul', id='stationList').findAll('li'):
        station = doc.createElement('station')

        id = doc.createElement('id')
        id.appendChild(doc.createTextNode(item.p['id']))
        station.appendChild(id)

        if item.p.has_key('class'):
            bus = doc.createElement('bus')
            station.appendChild(bus)

            color = doc.createElement('color')
            color.appendChild(doc.createTextNode(item.p['class']))
            bus.appendChild(color)
            
        name = doc.createElement('name')
        name.appendChild(doc.createTextNode(item.span.string))
        station.appendChild(name)

        mobileno = doc.createElement('mobileno')
        mobileno.appendChild(doc.createTextNode(item.em.string))
        station.appendChild(mobileno)

        stationList.appendChild(station)

    return doc

if __name__ == "__main__":
    f = open(sys.argv[1])
    xmlDoc = main(f)
    sys.stdout.write(xmlDoc.toprettyxml(indent='   ', encoding="utf-8"))
